<template>
  <div class="outer-container">
    <div class="nav-list">
      <navigation/>
    </div>
    
    <div class="main-container">
      <!-- 顶部个人信息卡片 -->
      <div class="profile-header">
        <div class="profile-card">
          <div class="profile-info">
            <div class="avatar-wrapper">
              <img src="@/assets/111.jpeg" alt="User Avatar" class="avatar">
              <div class="avatar-edit">
                <i class="fas fa-camera"></i>
              </div>
            </div>
            <div class="user-info">
              <h2 class="username">{{ userName }}</h2>
              <p class="user-bio">欢迎来到我的个人空间</p>
            </div>
          </div>
          <div class="stats-bar">
            <div class="stat-item">
              <span class="stat-value">{{ watchRecords.length }}</span>
              <span class="stat-label">观看记录</span>
            </div>
            <div class="stat-divider"></div>
            <div class="stat-item">
              <span class="stat-value">{{ favoriteItems.length }}</span>
              <span class="stat-label">我的收藏</span>
            </div>
            <div class="stat-divider"></div>
            <div class="stat-item">
              <span class="stat-value">0</span>
              <span class="stat-label">上传视频</span>
            </div>
          </div>
        </div>
      </div>

      <!-- 主要内容区域 -->
      <div class="content-section">
        <!-- 左侧菜单 -->
        <div class="side-menu">
          <div class="menu-group">
            <div class="menu-title">个人中心</div>
            <ul class="menu-list">
              <li :class="{ active: activeTab === 'profile' }">
                <router-link to="/profile">
                  <i class="fas fa-user"></i>
                  <span>个人主页</span>
                </router-link>
              </li>
              <li :class="{ active: activeTab === 'watchRecords' }">
                <a @click="showWatchRecords">
                  <i class="fas fa-history"></i>
                  <span>观看记录</span>
                </a>
              </li>
              <li :class="{ active: activeTab === 'favorites' }">
                <a @click="showFavorites">
                  <i class="fas fa-heart"></i>
                  <span>我的收藏</span>
                </a>
              </li>
            </ul>
          </div>

          <div class="menu-group">
            <div class="menu-title">创作中心</div>
            <ul class="menu-list">
              <li :class="{ active: activeTab === 'upload' }">
                <router-link to="/VideoUpload">
                  <i class="fas fa-upload"></i>
                  <span>上传视频</span>
                </router-link>
              </li>
              <li>
                <a @click="logout" class="logout-btn">
                  <i class="fas fa-sign-out-alt"></i>
                  <span>退出登录</span>
                </a>
              </li>
            </ul>
          </div>
        </div>

        <!-- 右侧内容区 -->
        <div class="main-content">
          <div class="content-header">
            <h2>{{ getContentTitle }}</h2>
            <div class="content-actions">
              <el-button v-if="activeTab === 'watchRecords'" size="small" type="danger" plain>
                <i class="fas fa-trash-alt"></i>
                清空记录
              </el-button>
              <el-button v-if="activeTab === 'favorites'" size="small" type="danger" plain>
                <i class="fas fa-heart-broken"></i>
                取消所有收藏
              </el-button>
            </div>
          </div>
          
          <!-- 观看记录 -->
          <div v-if="activeTab === 'watchRecords'" class="content-grid">
            <div v-for="(item, index) in watchRecords" 
                 :key="index" 
                 class="content-item"
                 @click="goLoadDetails(item.id, item.name)">
              <div class="item-cover">
                <img :src="item.cover" :alt="item.name">
                <div class="item-overlay">
                  <i class="fas fa-play-circle"></i>
                </div>
                <div class="item-badge">
                  <i class="fas fa-clock"></i>
                  2小时前观看
                </div>
              </div>
              <div class="item-info">
                <h3>{{ item.name }}</h3>
                <div class="item-meta">
                  <span class="meta-item">
                    <i class="fas fa-eye"></i>
                    1.2k
                  </span>
                  <span class="meta-item">
                    <i class="fas fa-heart"></i>
                    268
                  </span>
                </div>
              </div>
            </div>
          </div>

          <!-- 收藏记录 -->
          <div v-if="activeTab === 'favorites'" class="content-grid">
            <div v-for="(item, index) in favoriteItems" 
                 :key="index" 
                 class="content-item"
                 @click="goLoadDetails(item.id, item.name)">
              <div class="item-cover">
                <img :src="item.cover" :alt="item.name">
                <div class="item-overlay">
                  <i class="fas fa-play-circle"></i>
                </div>
                <div class="item-badge favorite">
                  <i class="fas fa-heart"></i>
                  已收藏
                </div>
              </div>
              <div class="item-info">
                <h3>{{ item.name }}</h3>
                <div class="item-meta">
                  <span class="meta-item">
                    <i class="fas fa-eye"></i>
                    2.5k
                  </span>
                  <span class="meta-item">
                    <i class="fas fa-star"></i>
                    9.2
                  </span>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, computed } from 'vue';
import Navigation from "@/components/navigation.vue";
import request from "@/utils/request";
import router from '@/router/router';
import axios from 'axios';

const watchRecords = ref([]);
const favoriteItems = ref([]);
const activeTab = ref('watchRecords');
const userName = localStorage.getItem('userName');

// 获取内容标题
const getContentTitle = computed(() => {
  switch (activeTab.value) {
    case 'watchRecords':
      return '观看记录';
    case 'favorites':
      return '我的收藏';
    case 'profile':
      return '个人主页';
    case 'upload':
      return '上传视频';
    default:
      return '';
  }
});

// 加载观看记录
const loadWatchRecords = async () => {
  const res = await request.post('/records/findAll', { username: userName });
  watchRecords.value = res.data.data;
};

// 加载收藏记录
const loadFavorites = async () => {
  const res = await request.post('/collect/findAll', { username: userName });
  favoriteItems.value = res.data.data;
};

// 切换到观看记录
const showWatchRecords = () => {
  activeTab.value = 'watchRecords';
  loadWatchRecords();
};

// 切换到收藏记录
const showFavorites = () => {
  activeTab.value = 'favorites';
  loadFavorites();
};

// 跳转到视频详情
const goLoadDetails = (id, name) => {
  router.push({ name: 'VideoStory', params: { id: id, name: name } });
};

// 登出
const logout = async () => {
  try {
    const token = localStorage.getItem('token');
    const config = {
      headers: {
        token: `${token}`,
        'Content-Type': 'application/json',
      },
    };

    await axios.get('http://localhost:9090/api/user/logout', config);
    localStorage.removeItem('isLoggedIn');
    localStorage.removeItem('userName');
    localStorage.removeItem('token');
    router.push('/login');
  } catch (error) {
    console.error('Logout failed:', error);
  }
};

// 默认加载观看记录
onMounted(() => {
  loadWatchRecords();
});
</script>

<style scoped>
.outer-container {
  min-height: 100vh;
  background-color: #f5f7fa;
}

.nav-list {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 60px;
  background-color: #1a1a1a;
  z-index: 1000;
}

.main-container {
  padding-top: 60px;
}

/* 顶部个人信息卡片 */
.profile-header {
  padding: 24px;
  background: linear-gradient(135deg, #1a1a1a 0%, #2d2d2d 100%);
}

.profile-card {
  max-width: 1200px;
  margin: 0 auto;
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
  border-radius: 16px;
  padding: 32px;
  color: #fff;
}

.profile-info {
  display: flex;
  align-items: center;
  gap: 24px;
  margin-bottom: 32px;
}

.avatar-wrapper {
  position: relative;
  width: 120px;
  height: 120px;
  border-radius: 50%;
  overflow: hidden;
  border: 4px solid rgba(255, 255, 255, 0.2);
}

.avatar {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.avatar-edit {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  height: 40px;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  opacity: 0;
  transition: all 0.3s ease;
}

.avatar-wrapper:hover .avatar-edit {
  opacity: 1;
}

.user-info {
  flex: 1;
}

.username {
  font-size: 24px;
  font-weight: 600;
  margin: 0 0 8px;
}

.user-bio {
  color: rgba(255, 255, 255, 0.7);
  margin: 0;
  font-size: 14px;
}

.stats-bar {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 48px;
}

.stat-divider {
  width: 1px;
  height: 40px;
  background: rgba(255, 255, 255, 0.1);
}

.stat-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
}

.stat-value {
  font-size: 24px;
  font-weight: 600;
  color: #e50914;
}

.stat-label {
  font-size: 14px;
  color: rgba(255, 255, 255, 0.7);
}

/* 主要内容区域 */
.content-section {
  max-width: 1200px;
  margin: 24px auto;
  display: flex;
  gap: 24px;
  padding: 0 24px;
}

.side-menu {
  width: 240px;
  flex-shrink: 0;
}

.menu-group {
  background: #fff;
  border-radius: 12px;
  padding: 16px;
  margin-bottom: 16px;
}

.menu-title {
  font-size: 16px;
  font-weight: 600;
  color: #333;
  margin-bottom: 16px;
  padding-left: 16px;
}

.menu-list {
  list-style: none;
  padding: 0;
  margin: 0;
}

.menu-list li {
  margin-bottom: 4px;
}

.menu-list li a {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  color: #666;
  text-decoration: none;
  border-radius: 8px;
  transition: all 0.3s ease;
}

.menu-list li.active a {
  background: #fff1f1;
  color: #e50914;
}

.menu-list li a:hover {
  background: #fff1f1;
  color: #e50914;
}

.menu-list li i {
  width: 20px;
  text-align: center;
}

.main-content {
  flex: 1;
  background: #fff;
  border-radius: 12px;
  padding: 24px;
}

.content-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
  padding-bottom: 16px;
  border-bottom: 1px solid #f0f2f5;
}

.content-header h2 {
  font-size: 20px;
  font-weight: 600;
  color: #333;
  margin: 0;
}

.content-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 24px;
}

.content-item {
  cursor: pointer;
  transition: all 0.3s ease;
}

.content-item:hover {
  transform: translateY(-4px);
}

.item-cover {
  position: relative;
  width: 100%;
  padding-top: 140%;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
}

.item-cover img {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.item-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.4);
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  transition: all 0.3s ease;
}

.item-overlay i {
  font-size: 48px;
  color: #fff;
  transform: scale(0.8);
  transition: all 0.3s ease;
}

.content-item:hover .item-overlay {
  opacity: 1;
}

.content-item:hover .item-overlay i {
  transform: scale(1);
}

.item-badge {
  position: absolute;
  top: 12px;
  right: 12px;
  background: rgba(0, 0, 0, 0.6);
  color: #fff;
  padding: 6px 12px;
  border-radius: 20px;
  font-size: 12px;
  display: flex;
  align-items: center;
  gap: 4px;
}

.item-badge.favorite {
  background: rgba(229, 9, 20, 0.8);
}

.item-info {
  padding: 12px 4px;
}

.item-info h3 {
  font-size: 14px;
  font-weight: 500;
  color: #333;
  margin: 0 0 8px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.item-meta {
  display: flex;
  align-items: center;
  gap: 12px;
}

.meta-item {
  display: flex;
  align-items: center;
  gap: 4px;
  color: #999;
  font-size: 12px;
}

.meta-item i {
  color: #e50914;
}

/* 响应式调整 */
@media (max-width: 768px) {
  .content-section {
    flex-direction: column;
  }

  .side-menu {
    width: 100%;
  }

  .profile-info {
    flex-direction: column;
    text-align: center;
  }

  .stats-bar {
    flex-wrap: wrap;
  }
}
</style>
  